//
//  DataBase.m
//  MMLOOK
//
//  Created by dllo on 15/9/5.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "DataBase.h"
#import "WedModel.h"

static sqlite3 *dbPoint = nil;

@implementation DataBase

//增加一条
+(BOOL)insertWithModel:(WedModel *)model
{
    //第一步 : 获得数据库指针
    sqlite3 *db = [self dbOpen];
    
    //第二步 : 获得数据库语句
    NSString *str = [NSString stringWithFormat:@"insert into collect(c_nameId,c_photo_path,c_like_count,c_price,c_introduce) values('%@','%@','%@','%@','%@')",model.nameId,model.photo_path,[model.like_count stringValue], [model.price stringValue],model.introduce];
    
    //第三步: 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    
    return [self judgeRustlt:result];
}

//删除一条
+(void)deleteWithModel:(WedModel *)model
{
    
    sqlite3 *db = [self dbOpen];
    
    NSString *str = [NSString stringWithFormat:@"delete from collect where c_nameId = '%@'", model.nameId];
    
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    
    [self judgeRustlt:result];
}


//查询一条数据
+(BOOL)selectWith:(NSString *)nid
{
    
    //获得数据库指针
    sqlite3 *db = [self dbOpen];
    
    //获得数据库替身
    sqlite3_stmt *stmt = nil;
    
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select * from collect where c_nameId = '%@'", nid];
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            
            sqlite3_finalize(stmt);
            
            return YES;
            
        }
    }
    
    // 回收状态指针的内存, 将sql的改变结果保存到数据库 (释放替身指针)
    sqlite3_finalize(stmt);
    
    return NO;
    
}



//查询全部数据
+(NSArray *)selectAll
{
    
    // 建立一个空数组, 用来装数据
    NSMutableArray *array = [NSMutableArray array];
    
    //获得数据库指针
    sqlite3 *db = [self dbOpen];
    
    //获得数据库替身
    sqlite3_stmt *stmt = nil;
    
    //数据库语句
    NSString *str = [NSString stringWithFormat:@"select * from collect"];
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            const unsigned char *cId = sqlite3_column_text(stmt, 0);
            
            const unsigned char *cphoto = sqlite3_column_text(stmt, 1);
            
            const unsigned char *clike = sqlite3_column_text(stmt, 2);
            
            const unsigned char *cprice = sqlite3_column_text(stmt, 3);
            
            const unsigned char *cintroduce = sqlite3_column_text(stmt, 4);
            
            
            NSString *cid = [NSString stringWithUTF8String:(const char *)cId];
            
            NSString *photo = [NSString stringWithUTF8String:(const char *)cphoto];
            
            NSString *like = [NSString stringWithUTF8String:(const char *)clike];
            
            NSString *price = [NSString stringWithUTF8String:(const char *)cprice];
            
            NSString *introduce = [NSString stringWithUTF8String:(const char *)cintroduce];
            
            WedModel *model = [[WedModel alloc]init];
            model.nameId = cid;
            model.photo_path = photo;
            
            model.like_count = [NSNumber numberWithInt:[like intValue]];
            
            model.price = [NSNumber numberWithFloat:[price floatValue]];
            
            model.introduce = introduce;
            
            [array addObject:model];
            
            [model release];
            
        }
    }
    
    // 回收状态指针的内存, 将sql的改变结果保存到数据库 (释放替身指针)
    sqlite3_finalize(stmt);
    
    return array;
    
}



//判断
+(BOOL)judgeRustlt:(NSInteger)result
{
    if (result == SQLITE_OK) {
        
        NSLog(@"执行成功");
        
        return YES;
        
    } else {
        
        NSLog(@"执行失败 result = %ld", result);
        
        return NO;
    }
    
}




+ (sqlite3 *)dbOpen
{
    
    //如果指针存在 就返回
    if (dbPoint) {
        
        return dbPoint;
    }
    
    
    NSFileManager *file = [NSFileManager defaultManager];
    
    NSString *pathOld = [[NSBundle mainBundle]pathForResource:@"Marry" ofType:@"rdb"];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
    
    NSString *pathNew = [NSString stringWithFormat:@"%@/%@", path, @"Marry.rdb"];
    
    NSLog(@"path = %@", pathNew);
    
    
    if ([file fileExistsAtPath:pathNew]) {
        
        sqlite3_open([pathNew UTF8String], &dbPoint);
        
        
    } else {
        
        
        [file copyItemAtPath:pathOld toPath:pathNew error:nil];
        
        
    }
    
    sqlite3_open([pathNew UTF8String], &dbPoint);
    
    return dbPoint;
    
}













+(void)dbClose
{
    sqlite3_close(dbPoint);
}


@end
